import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.font_manager as fm
import seaborn as sns
import chart
import data_extract

plt.rcParams['font.sans-serif'] = ['SimHei'] 
plt.rcParams["axes.unicode_minus"] = False

# 创建GUI界面
class ChartSelector:
    def __init__(self, root, bmi_data, year_data, state_data, height_BLK_data, cluster_data_ows_dws, weight_TRB_data, PTS_data, height_TRB_data, bmi_TRB_data, position_stats, stats_columns):
        self.root = root
        self.root.title("NBA数据可视化")
        self.root.geometry("800x200")
        
        self.bmi_data = bmi_data
        self.year_data = year_data
        self.state_data = state_data
        self.height_BLK_data = height_BLK_data
        self.cluster_data_ows_dws=cluster_data_ows_dws
        self.weight_TRB_data=weight_TRB_data
        self.PTS_data=PTS_data
        self.height_TRB_data=height_TRB_data
        self.bmi_TRB_data=bmi_TRB_data
        self.position_stats = position_stats
        self.stats_columns = stats_columns
        
        # 创建标签
        label = ttk.Label(root, text="请选择要显示的图表:", font=("Arial", 12))
        label.pack(pady=10)
        
        # 创建按钮框架
        button_frame = ttk.Frame(root)
        button_frame.pack(pady=20)
        
        # 创建按钮
        bmi_button = ttk.Button(button_frame, text="BMI分布", command=self.show_bmi_chart)
        bmi_button.grid(row=0, column=0, padx=10, pady=5)
        
        year_button = ttk.Button(button_frame, text="出生年份", command=self.show_year_chart)
        year_button.grid(row=0, column=1, padx=10, pady=5)
        
        state_button = ttk.Button(button_frame, text="出生州", command=self.show_state_chart)
        state_button.grid(row=0, column=2, padx=10, pady=5)
        
        HBLK_button = ttk.Button(button_frame, text="身高与盖帽率", command=self.show_height_BLK_chart)
        HBLK_button.grid(row=0, column=3, padx=10, pady=5)
        
        OWDS_button = ttk.Button(button_frame, text="球员类型的聚类分析", command=self.show_ows_dws_cluster_chart)
        OWDS_button.grid(row=0, column=4, padx=10, pady=5)
        
        WTRB_button = ttk.Button(button_frame, text="体重和篮板率", command=self.show_weight_TRB_chart)
        WTRB_button.grid(row=1, column=0, padx=10, pady=5)
        
        HTRB_button = ttk.Button(button_frame, text="身高与篮板率", command=self.show_height_TRB_chart)
        HTRB_button.grid(row=1, column=1, padx=10, pady=5)
        
        BTRB_button = ttk.Button(button_frame, text="bmi与篮板率", command=self.show_bmi_TRB_chart)
        BTRB_button.grid(row=1, column=2, padx=10, pady=5)
        
        PTS_button = ttk.Button(button_frame, text="得分分布", command=self.show_PTS_chart)
        PTS_button.grid(row=1, column=3, padx=10, pady=5)
        
        position_button = ttk.Button(button_frame, text="位置雷达图", command=self.show_position_selector)
        position_button.grid(row=1, column=4, padx=10, pady=5)
        
        button_frame.columnconfigure(0, weight=1)
        button_frame.columnconfigure(1, weight=1)
        # 退出按钮
        exit_button = ttk.Button(root, text="退出", command=root.quit)
        exit_button.pack(pady=10)
    
    def show_bmi_chart(self):
        """显示BMI图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("BMI分布")
        
        fig = chart.create_bmi_chart(self.bmi_data)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
    
    def show_year_chart(self):
        """显示出生年份图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("出生年份分布")
        
        fig = chart.create_birth_year_chart(self.year_data)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
    
    def show_state_chart(self):
        """显示出生州图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("出生州分布")
        
        fig = chart.create_birth_state_chart(self.state_data)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()

    def show_height_BLK_chart(self):
        """显示身高与盖帽率图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("身高与盖帽率相关性")
        
        fig = chart.create_height_BLK_chart(self.height_BLK_data)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_ows_dws_cluster_chart(self):
        """显示OWS和DWS聚类分析图表"""
        # 执行聚类分析
        data_with_clusters, cluster_centers = data_extract.perform_ows_dws_clustering(self.cluster_data_ows_dws)
        
        chart_window = tk.Toplevel(self.root)
        chart_window.title("OWS和DWS聚类分析")
        chart_window.geometry("1000x700")  # 设置更大的窗口大小
        
        fig = chart.create_ows_dws_cluster_chart(data_with_clusters, cluster_centers)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_weight_TRB_chart(self) :
        """显示体重和篮板率图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("体重和篮板率相关性")
    
        fig = chart.create_weight_TRB_chart(self.weight_TRB_data)
        canvas = FigureCanvasTkAgg(fig,master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_height_TRB_chart(self):
        """显示身高和篮板率图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("身高和篮板率相关性")
    
        fig = chart.create_height_TRB_chart(self.height_TRB_data)
        canvas = FigureCanvasTkAgg(fig,master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_bmi_TRB_chart(self):
        """显示bmi和篮板率图表"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("bmi和篮板率相关性")
    
        fig = chart.create_bmi_TRB_chart(self.bmi_TRB_data)
        canvas = FigureCanvasTkAgg(fig,master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_PTS_chart(self) :
        """显示得分分布直方图"""
        chart_window = tk.Toplevel(self.root)
        chart_window.title("得分分布直方图")
    
        fig = chart.create_PTS_chart(self.PTS_data)
        canvas = FigureCanvasTkAgg(fig,master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
    def show_position_selector(self):
        """显示位置选择对话框"""
        selector_window = tk.Toplevel(self.root)
        selector_window.title("选择位置")
        selector_window.geometry("300x200")
    
        label = ttk.Label(selector_window, text="请选择要查看的位置:", font=("Arial", 12))
        label.pack(pady=20)
    
        position_var = tk.StringVar()
        available_positions = [pos for pos in self.position_stats.keys() if (pos != 'F-C' and pos != 'C-F'and pos != 'G-F'and pos != 'F-G')]
        position_combo = ttk.Combobox(selector_window, textvariable=position_var, values=available_positions)
        position_combo.pack(pady=10)
    
        # 确认按钮
        confirm_button = ttk.Button(selector_window, text="确认", command=lambda: self.show_position_chart(position_var.get(), selector_window))
        confirm_button.pack(pady=10)

    def show_position_chart(self, position, selector_window):
        """显示指定位置的雷达图"""
        if not position:
           return
    
        # 关闭选择窗口
        selector_window.destroy()
    
        # 创建雷达图窗口
        chart_window = tk.Toplevel(self.root)
        chart_window.title(f"{position}位置球员雷达图")
        chart_window.geometry("800x600")

        # 创建雷达图
        fig = chart.create_position_radar_chart(self.position_stats, self.stats_columns, position)
        canvas = FigureCanvasTkAgg(fig, master=chart_window)
        canvas.draw()
        canvas.get_tk_widget().pack()
        
# 主程序
if __name__ == "__main__":
    try:
        # 处理数据
        bmi_data, year_data, state_data, height_BLK_data,cluster_data,weight_TRB_data,PTS_data,height_TRB_data,bmi_TRB_data= data_extract.process_data()
        position_stats, stats_columns = data_extract.get_position_stats()
        # 创建GUI
        root = tk.Tk()
        app = ChartSelector(root, bmi_data, year_data, state_data, height_BLK_data, cluster_data,weight_TRB_data,PTS_data,height_TRB_data,bmi_TRB_data,position_stats, stats_columns)
        root.mainloop()
        
    except FileNotFoundError:
        print("错误：找不到Player_cleaned.csv文件")
        print("请确保Player_cleaned.csv文件位于当前目录下")
        
        # 创建文件选择对话框
        root = tk.Tk()
        root.withdraw()  # 隐藏主窗口
